package lt.liepasoft.locr.data;

public class RgbImage implements Image {
	
   /**
     * Width of the image, in pixels.
     */
    public final int width;
    /**
     * Height of the image, in pixels.
     */
    public final int height;
    /**
     * Total number of pixels in the image (<code>width*height</code>).
     */
    public final int npix;
    /**
     * Aspect ratio of the image (<code>width/height</code>).
     */
    public final float aspectRatio;
    
    public final Bounds bounds;
    
    /**
     * An array of pixels.  This can be in RGBA or grayscale.
     * By default, it is RGBA, but if the <code>toGrayScale()</code> method
     * has been called, each pixel will be in the range of 0-255 grayscale.
     */
    public final int[] pixels;

    /**
     * Construct a new <code>PixelImage</code> object from an array of
     * pixels.
     * @param pixels An array of pixels.  This can be in RGBA or grayscale.
     * By default, it is RGBA, but if the <code>toGrayScale()</code> method
     * has been called, each pixel will be in the range of 0-255 grayscale.
     * @param width Width of the image, in pixels.
     * @param height Height of the image, in pixels.
     */
    public RgbImage(int[] pixels, int width, int height){
        this.width = width;
        this.height = height;
        this.npix = width * height;
        this.aspectRatio = ((float) width) / ((float) height);
        bounds = new Bounds(0, 0, width, height);
        this.pixels = pixels;       
    }
    
    /**
     * Get the value of a pixel at a specific <code>x,y</code> position.
     * @param x The pixel's x position.
     * @param y The pixel's y position.
     * @return The value of the pixel.
     */
    public final int getPixel(int x, int y){
        return pixels[getPixelIndex(x,y)];
    }
    
    public final int getPixel(int index){
    	return pixels[index];
    }
    
    /**
     * Get the index of a pixel at a specific <code>x,y</code> position.
     * @param x The pixel's x position.
     * @param y The pixel's y position.
     * @return The pixel index (the index into the <code>pixels</code> array)
     * of the pixel.
     */
    public int getPixelIndex(int x, int y){
        return (y * width) + x;
    }    
    
    public float getAspectRatio() {
        return aspectRatio;
    }

    public int getHeight() {
        return height;
    }

    public int getNpix() {
        return npix;
    }
    
    public int getWidth() {
        return width;
    }
    
    public int getRow(int index){
        return index/width;
    }
    
    public int getColumn(int index){
        return index%width;
    }    
}
